package com.didispace;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class TraceApplication {

	private final Logger logger = Logger.getLogger(getClass());

	// 通过创建 AlwaysSampler 的 Bean (它实现的 isSampled 方法始终返回 true) 来覆盖默认的 PercentageBasedSampler 策略  p393
	// @Bean
	// public AlwaysSampler defaultSampler() {
	// 	return new AlwaysSampler();
	// }

	// 实现一个仅包含指定 Tag 的抽样策略  p393
	// @Bean
	// public TagSampler tagSampler() {
	// 	// return new TagSampler("Trace");
	// 	return new TagSampler(null);
	// }

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@RequestMapping(value = "/trace-1", method = RequestMethod.GET)
	public String trace() {
		logger.info("===<call trace-1>===");
		return restTemplate().getForEntity("http://trace-2/trace-2", String.class).getBody();
	}

	public static void main(String[] args) {
		SpringApplication.run(TraceApplication.class, args);
	}

}
